
package com.hexinfo.dmpro.script.config.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.common.model.ClusterSparingConf;
import com.hexinfo.dmpro.common.service.ClusterSparingConfService;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyParam;
import com.hexinfo.dmpro.script.config.dao.ConfigPropertyParamMapper;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyParamService;
import com.hexinfo.dmpro.script.pack.entity.ScriptPackScanListEntity;
import com.hexinfo.dmpro.script.pack.entity.ScriptPackTaskEntity;
import liquibase.pro.packaged.C;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package com.hexinfo.dmpro.script.config.service.impl
 * @ClassName ConfigPropertyParamServiceImpl
 * @Description 参数化配置param表
 * @Author le.zhang
 * @Date 2024/3/4 13:10
 * @Version v1.0
 **/
@Service
public class ConfigPropertyParamServiceImpl extends ServiceImpl<ConfigPropertyParamMapper, ConfigPropertyParam> implements ConfigPropertyParamService {

    private final ClusterSparingConfService clusterSparingConfService;

    public ConfigPropertyParamServiceImpl(ClusterSparingConfService clusterSparingConfService) {
        this.clusterSparingConfService = clusterSparingConfService;
    }
/**
 * @Method queryList
 * @Param page
 * @Param configPropertyParam
 * @Return com.hexinfo.dmpro.common.base.PageResult
 * @Description 分页查询
 * @Author le.zhang
 * @Date 2024/3/4 13:10
 * @Version V1.0
 */
    @Override
    public PageResult queryList(Page page, ConfigPropertyParam configPropertyParam) {
        LambdaQueryWrapper<ConfigPropertyParam> wrapper = beforeQuery(configPropertyParam);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        List<ConfigPropertyParam> configPropertyParams = returnPage.getRecords();
        afterQuery(returnPage.getRecords());
        return new PageResult(configPropertyParams, returnPage.getTotal());
    }

/**
 * @Method beforeQuery
 * @Param configPropertyParam
 * @Return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.hexinfo.dmpro.script.config.model.ConfigPropertyParam>
 * @Description 分页前置查询 通过集群id精确查
 * @Author le.zhang
 * @Date 2024/3/4 13:10
 * @Version V1.0
 */
    private LambdaQueryWrapper<ConfigPropertyParam> beforeQuery(ConfigPropertyParam configPropertyParam){
        LambdaQueryWrapper<ConfigPropertyParam> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(configPropertyParam.getClusterId())) {
            wrapper.eq(ConfigPropertyParam::getClusterId, configPropertyParam.getClusterId());
        }
        return wrapper;
    }

/**
 * @Method afterQuery
 * @Param configPropertyParamList
 * @Return void
 * @Description 分页后置查询 将集群id转换为集群名称存入对象中
 * @Author le.zhang
 * @Date 2024/3/4 13:10
 * @Version V1.0
 */
    private void afterQuery(List<ConfigPropertyParam> configPropertyParamList) {
        for (ConfigPropertyParam configPropertyParam : configPropertyParamList) {
            ClusterSparingConf byId = clusterSparingConfService.getById(configPropertyParam.getClusterId());
            if (byId != null) {
                configPropertyParam.setClusterName(byId.getClusterName());
            }
        }
    }
    
/**
 * @Method queryListNoPage
 * @Param configPropertyParam
 * @Return java.util.List<com.hexinfo.dmpro.script.config.model.ConfigPropertyParam>
 * @Description 不分页查询，通过集群id
 * @Author le.zhang
 * @Date 2024/3/4 13:10
 * @Version V1.0
 */
    @Override
    public List<ConfigPropertyParam> queryListNoPage(ConfigPropertyParam configPropertyParam) {
        LambdaQueryWrapper<ConfigPropertyParam> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(configPropertyParam.getClusterId()), ConfigPropertyParam::getClusterId, configPropertyParam.getClusterId());
        List<ConfigPropertyParam> configPropertyParams = this.baseMapper.selectList(wrapper);
        afterQuery(configPropertyParams);
        return configPropertyParams;
    }
    
    
/**
 * @Method getListByClusterId
 * @Param clusterId
 * @Return java.util.List<com.hexinfo.dmpro.script.config.model.ConfigPropertyParam>
 * @Description 通过集群id查询返回ConfigPropertyParam类型List集合
 * @Author le.zhang
 * @Date 2024/3/4 13:10
 * @Version V1.0
 */
    @Override
    public List<ConfigPropertyParam> getListByClusterId(String clusterId) {
        ConfigPropertyParam configPropertyParam = new ConfigPropertyParam();
        configPropertyParam.setClusterId(clusterId);
        return queryListNoPage(configPropertyParam);
    }

/**
 * @Method findAll
 * @Param
 * @Return java.util.List<com.hexinfo.dmpro.script.config.model.ConfigPropertyParam>
 * @Description json配置文件到数据库后获取所有数据
 * @Author le.zhang
 * @Date 2024/3/4 13:11
 * @Version V1.0
 */
    public List<ConfigPropertyParam> findAll(){
        LambdaQueryWrapper<ConfigPropertyParam> wrapper = Wrappers.lambdaQuery();
        List<ConfigPropertyParam> configPropertyParamList = this.baseMapper.selectList(wrapper);
        return configPropertyParamList;
    }
}
